﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Collections;

namespace TooltracePC
{
    class dgv
    {
        private static DataTable _dtSelected = new DataTable();
        private static DataGridView _dgv = new DataGridView();
        private static DataGridView _gdgv = new DataGridView();

        private static int _idSelected = 0;
        private static string _SelectedFile = "";

        public void setSelectID(string senID)
        {
            _idSelected = int.Parse(senID);
        }

        public int getSelectID()
        {
            return _idSelected;
        }

        public void setSelectedFile(string filename)
        {
            _SelectedFile = filename.Trim();
        }

        public string getSelectedFile()
        {
            return _SelectedFile;
        }


        public void setgDgv(DataGridView sender)
        {
            if (sender != null)
            {
                _gdgv = sender;
            }
        }

        public void setgDataSource(DataTable dt)
        {
            if (_gdgv != null)
            {
                //_dtSelected = dt;
                _gdgv.DataSource = dt;
            }
        }




        public void setDgv(DataGridView sender)
        {
            if(sender != null)
            {
                _dgv = sender;
            }
        }

      
        
        //get init
        public void setFormatComm()
        {
            if (_dgv != null)
            {
                //_dgv.TopLeftHeaderCell.Value = "全选";
                _dgv.Columns[0].ReadOnly = true;
                //disable sort
                for (int i = 0; i < _dgv.Columns.Count; i++)
                {
                    _dgv.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                }
                _dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
        }


        //set row format
        public void setRowFormat()
        {
            if(_dgv.Rows.Count == 0)
            {
                if (_gdgv.Rows.Count > 0) { _dgv = _gdgv; }
            }

            if (_dgv != null)
            {
                if (_dgv.Rows.Count != 0)
                {
                    for (int i = 0; i < _dgv.Rows.Count;)
                    {
                        _dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.GhostWhite;
                        if (i + 1 < _dgv.Rows.Count)
                        {
                            _dgv.Rows[i + 1].DefaultCellStyle.BackColor = System.Drawing.Color.Gainsboro;
                        }
                        i += 2;
                    }
                }
            }
        }

        //set sync format
        public void setSyncRowFormat()
        {
            if (_dgv != null)
            {
                if (_dgv.Rows.Count > 0)
                {
                    for (int i = 0; i < _dgv.Rows.Count; i++)
                    {
                        if(_dgv.Rows[i].Cells[5].Value.ToString() == "GREEN")
                        {
                            _dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.GreenYellow;
                        }
                        else if (_dgv.Rows[i].Cells[5].Value.ToString() == "RED")
                        {
                            _dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.OrangeRed;  
                        }
                        else if (_dgv.Rows[i].Cells[5].Value.ToString() == "CYAN")
                        {
                            _dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.MediumVioletRed;
                        }
                        else if (_dgv.Rows[i].Cells[5].Value.ToString() == "YELLOW")
                        {
                            _dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
                        }
                    }
                }
            }
        }

        //set sync data row format
        //set sync format
        public void setSyncDataRowFormat()
        {
            if (_dgv != null)
            {
                if (_dgv.Rows.Count > 0)
                {
                    for (int i = 0; i < _dgv.Rows.Count; i++)
                    {
                        if (_dgv.Rows[i].Cells[12].Value.ToString() == "G")
                        {
                            _dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.GreenYellow;
                        }
                       else if (_dgv.Rows[i].Cells[12].Value.ToString() == "R")
                        {
                            _dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.OrangeRed;
                        }
                        else if (_dgv.Rows[i].Cells[12].Value.ToString() == "Y")
                        {
                            _dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
                        }
                    }
                }
            }
        }



        //set tool format
        public void setFormatTool()
        {
            if (_dgv != null)
            {
                _dgv.Columns[0].Width = 150;
                _dgv.Columns[0].HeaderText = "编号";
                _dgv.Columns[1].HeaderText = "标签ID";
                _dgv.Columns[2].HeaderText = "标签名称";
                _dgv.Columns[3].HeaderText = "标签编码";
            }
        }


        public void setFormatStaff()
        {
            if (_dgv != null)
            {
                _dgv.Columns[0].Width = 150;
                _dgv.Columns[0].HeaderText = "编号";
                _dgv.Columns[1].HeaderText = "用户编码";
                _dgv.Columns[2].HeaderText = "用户名称";
            }
        }

        public void setFormatSync()
        {
            if (_dgv != null)
            {
                _dgv.Columns[0].Width = 150;
                _dgv.Columns[0].HeaderText = "出库人";
                _dgv.Columns[1].Width = 200;
                _dgv.Columns[1].HeaderText = "日期";
                _dgv.Columns[2].Width = 200;
                _dgv.Columns[2].HeaderText = "时间";
                _dgv.Columns[4].HeaderText = "名称";
                _dgv.Columns[3].HeaderText = "状态";

                _dgv.Columns[5].HeaderText = "";
                _dgv.Columns[5].Width = 0;

            }
        }


        public void setFormatSyncData()
        {
            if (_dgv != null)
            {
                //_dgv.Columns[0].Width = 400;
                _dgv.Columns[1].Width = 100;
                _dgv.Columns[2].Width = 100;
                _dgv.Columns[3].Width = 60;
                //_dgv.Columns[4].Width = 300;
                _dgv.Columns[5].Width = 80;
                //_dgv.Columns[6].Width = 300;
                _dgv.Columns[7].Width = 80;
                //_dgv.Columns[8].Width = 300;
                _dgv.Columns[9].Width = 60;
                //_dgv.Columns[10].Width = 300;
                _dgv.Columns[11].Width = 120;

                _dgv.Columns[12].HeaderText = "";
                _dgv.Columns[12].Width = 1;

            }
        }



        public void setDataSource(DataTable dt)
        {
            if (_dgv != null)
            {
                _dtSelected = dt;
                _dgv.DataSource = _dtSelected;
            }   
        }


        //get selected rows
        public int[] getSelectedRows()
        {
            int[] _id = null;
            int nRowCount = 0;

            if (_dgv == null)
            {
                return _id;
            }

            nRowCount = _dgv.SelectedRows.Count;
            _id = new int[nRowCount];
            for (int i = 0; i < nRowCount; i++)
            {
                _id[i] = int.Parse(_dgv.SelectedRows[i].Cells[0].Value.ToString());
            }

            return _id;
        }




    }
}
